package com.htrq.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htrq.dao.TResDao;
import com.htrq.entity.Page;
import com.htrq.entity.TResource;
import com.htrq.entity.TresTree;

@Service
@Transactional(value="txManager")
public class TResService {
	@Resource
	private TResDao tresDao;

	// 不带checkbox的菜单数据
	public List<TResource> getRight(List<TResource> list2) {
		List<TResource> list = new ArrayList<TResource>();
		for (TResource tr : list2) {
			if (tr.getIfc() == 0) {
				list.add(tr);
			}
		}
		for (TResource tr : list) {
			for (TResource tr1 : list) {
				if (tr.getId() == tr1.getParentId()) {
					tr.setLeaf(false);
				}
			}
		}
		return TResource.buildTree(list);
	}
	// 带checkbox的数据
	public List<TresTree> getRightByBox() {
		List<TResource> list = tresDao.getRight();
		List<TresTree> list1 = new ArrayList<TresTree>();
		for (TResource tr : list) {
			for (TResource tr1 : list) {
				if (tr.getId() == tr1.getParentId()) {
					tr.setLeaf(false);
				}
			}
			TresTree tree = new TresTree();
			tree.setId(tr.getId());
			tree.setChecked(false);
			tree.setLeaf(tr.isLeaf());
			tree.setParentId(tr.getParentId());
			tree.setText(tr.getText());
			tree.setHrefTarget(tr.getHrefTarget());
			tree.setIfc(tr.getIfc());
			tree.setRemark(tr.getRemark());
			list1.add(tree);
		}
		return TResource.buildTree(list1);
	}
	public boolean saveres(TResource resc) {
		try {
			tresDao.add(resc);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public List<TResource> getAllByPage(Page page) {
		return tresDao.getAllByPage(page);
	}
	public Long getTotal() {
		return tresDao.getTotal();
	}
}
